package yunhe.servlet;

import yunhe.biz.ISaleBiz;
import yunhe.biz.impl.SaleBizImpl;
import yunhe.entity.*;
import yunhe.util.GeneralUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/6/20.
 */
@WebServlet(name = "PackAddServlet",urlPatterns = "/sale/PackAddServlet")
public class PackAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        ISaleBiz iSaleBizImpl = new SaleBizImpl();

        String salPackPayType=request.getParameter("salPackPayType");
        String salPackSignTime=request.getParameter("salPackSignTime");
        String proBarCode=request.getParameter("proBarCode");
        String cusId=request.getParameter("cusId");

        String spp=request.getParameter("salPackPrice");
        System.out.println(spp);
        Double salPackPrice=Double.parseDouble(spp);
        String sppt=request.getParameter("salPackPayTime");
        int salPackPayTime=Integer.parseInt(sppt);
        String spt=request.getParameter("salPackTerm");
        int salPackTerm=Integer.parseInt(spt);

        try {
            SalePack salePack=new SalePack();
            Customer customer = new Customer();
            Product product=new Product();

            customer.setCusId(cusId);
            product.setProBarCode(proBarCode);

            Date salPackSignTime2=GeneralUtil.stringToUtilDate(salPackSignTime,"MM/dd/yyyy");



            String seq= GeneralUtil.creatSeq("sp");

            salePack.setProduct(product);
            salePack.setSalPackId(seq);
            salePack.setCustomer(customer);
            salePack.setSalPackPrice(salPackPrice);
            salePack.setSalPackTerm(salPackTerm);
            salePack.setSalPackPayType(salPackPayType);
            salePack.setSalPackPayTime(salPackPayTime);
            salePack.setSalPackSignTime(salPackSignTime2);

            int flag = iSaleBizImpl.salePackGenerate(salePack);

            if (flag != 0) {
                System.out.println("新增销售合同成功");
                response.sendRedirect("successful.jsp");
            }
        }catch (Exception e){
            PrintWriter out=response.getWriter();
            response.sendRedirect("failed.jsp");
        }
    }
}
